package com.ljc.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljc.dto.Result;
import com.ljc.entity.DeviceUtilization;
import com.ljc.service.DeviceUtilizationService;
import com.ljc.util.JWTUtil;
import com.ljc.vo.DeviceUtilizationSearch;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.lang.model.element.VariableElement;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("deviceUtilization")
@CrossOrigin
@Slf4j
public class DeviceUtilizationController {

    @Autowired
    private DeviceUtilizationService deviceUtilizationService;
    @RequestMapping("add")
    public Result add(HttpServletRequest request, @RequestBody DeviceUtilization deviceUtilization){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        deviceUtilization.setUserId(Integer.valueOf(id));
        log.info("[{}]",deviceUtilization);
        try {
            deviceUtilizationService.add(deviceUtilization);
            result.setStatus(1).setMsg("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(0).setMsg("新增失败");
        }
        return result;
    }
    @RequestMapping("searchByUser")
    public Result searchByUser(HttpServletRequest request,@RequestBody DeviceUtilizationSearch search){      Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        search.setUserId(Integer.valueOf(id));
        PageHelper.startPage(search.getPageNum(),search.getPageSize());
        List<DeviceUtilization> deviceUtilizations = deviceUtilizationService.searchByUser(search);
        PageInfo<DeviceUtilization> deviceUtilizationPageInfo = new PageInfo<>(deviceUtilizations);
        result.setStatus(1).setMsg("查询用户的添加的设备利用率成功").setData(deviceUtilizationPageInfo);
        return  result;
    }
    @RequestMapping("getOne")
    public Result getOne(@RequestParam int id){
        Result result = new Result();
        DeviceUtilization one = deviceUtilizationService.getOne(id);
        result.setStatus(1).setMsg("根据Id查询成功").setData(one);
        return result;
    }
    @RequestMapping("update")
    private Result update(@RequestBody DeviceUtilization deviceUtilization){
        Result result = new Result();
        try {
            deviceUtilizationService.update(deviceUtilization);
            result.setStatus(1).setMsg("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(0).setMsg("修改失败");
        }
        return result;
    }
    @RequestMapping("delete")
    public Result delete(@RequestParam int id){
        Result result = new Result();
        try {
            deviceUtilizationService.delete(id);
            result.setStatus(1).setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(0).setMsg("删除失败");
        }
        return result;
    }
    @RequestMapping("searchByAdmit")
    public Result searchByAdmit(HttpServletRequest request,@RequestBody DeviceUtilizationSearch search){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        search.setAdmitId(Integer.valueOf(id));
        PageHelper.startPage(search.getPageNum(),search.getPageSize());
        List<DeviceUtilization> deviceUtilizations = deviceUtilizationService.searchByAdmit(search);
        PageInfo<DeviceUtilization> deviceUtilizationPageInfo = new PageInfo<>(deviceUtilizations);
        result.setStatus(1).setMsg("查询所有用户设备利用率成功").setData(deviceUtilizationPageInfo);
        return  result;
    }
    @RequestMapping("statistic")
    public Result statistic(HttpServletRequest request,@RequestBody DeviceUtilizationSearch deviceUtilizationSearch){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        deviceUtilizationSearch.setAdmitId(Integer.valueOf(id));
        List<DeviceUtilization> deviceUtilizations = deviceUtilizationService.searchByAdmit(deviceUtilizationSearch);
        int quantity=0;
        float utilizations=0;
        for (DeviceUtilization device : deviceUtilizations) {
            quantity+=device.getDeviceQuantity();
            utilizations+=device.getDeviceQuantity()*device.getUtilization().floatValue();
        }
        float avg = utilizations / quantity;
        result.setMsg("统计成功").setStatus(1).setData(avg);
        return result;
    }
}
